<?php

namespace App\Store\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Store\Models\Basic\TemplateTag;
use Illuminate\Support\Facades\Redis;

class TemplateController extends Controller
{

    public function savetemplate(Request $request){
        $template_id=intval($request->template_id);
        $report_id=strval($request->report_id);
        $secretkey=strval($request->secretkey);
        if($template_id<=0){
            return response()->json(['code' => '001', 'msg' =>'模板ID参数错误']); 
        }

        if($report_id==''){
            return response()->json(['code' => '001', 'msg' =>'模板标识不能为空']);
        }

        if($secretkey!=env('SECRETKEY')){
            return response()->json(['code' => '001', 'msg' =>'密钥错误']);
        }

        $template=TemplateTag::where('id',$template_id)->first();
        if(!$template){
            return response()->json(['code' => '002', 'msg' =>'模板ID不存在']); 
        }

        TemplateTag::where('id',$template_id)->update(['report_id'=>$report_id]);
        
        return response()->json(['code' => '000', 'msg' =>'保存成功']);
    }

    public function templatedata(Request $request){
        $tag_token=$request->tag_token;
        $secretkey=strval($request->secretkey);
        if($tag_token==''){
            return response()->json(['code' => '001', 'msg' =>'打印数据标识不能为空']); 
        }

        if($secretkey!=env('SECRETKEY')){
            return response()->json(['code' => '001', 'msg' =>'密钥错误']);
        }

        $data=Redis::get($tag_token);

        $data=is_array(json_decode($data,true))?json_decode($data,true):[];


        return response()->json(['code' => '000', 'msg' =>'获取成功','data'=>['goodsinfolist'=>$data]]);

    }
}